/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.iec.edp.caf.rpc.grpc.test;

import io.iec.edp.caf.commons.event.config.EventListenerSettings;
import io.iec.edp.caf.rpc.api.event.RpcServerEventBroker;
import io.iec.edp.caf.rpc.api.event.RpcServerEventManager;
import io.iec.edp.caf.rpc.api.filter.RpcClientFilter;
import io.iec.edp.caf.rpc.api.filter.RpcServerFilter;
import io.iec.edp.caf.rpc.api.service.InternalServiceManageService;
import io.iec.edp.caf.rpc.api.utils.DefinitionUtil;
import io.iec.edp.caf.rpc.grpc.test.impl.ConstVirable;
import io.iec.edp.caf.rpc.grpc.test.impl.RpcTest;
import io.iec.edp.caf.rpc.grpc.test.impl.RpcTestImpl;
import io.iec.edp.caf.rpc.remote.grpc.filter.GrpcClientFilter;
import io.iec.edp.caf.rpc.remote.grpc.filter.GrpcServerFilter;
import lombok.var;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.util.stream.Collectors;

import static io.iec.edp.caf.rpc.grpc.test.impl.ConstVirable.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@Configuration
@EnableAutoConfiguration
@ComponentScan({"io.iec.edp.caf.rpc.grpc"})
public class RpcTestConfiguration {

    @Bean
    public RpcServerFilter cafGrpcServerInterceptor(){
        return new GrpcServerFilter();
    }

    @Bean
    public RpcClientFilter cafGrpcClientInterceptor(){
        return new GrpcClientFilter();
    }
    @Bean
    public RpcServerEventManager createRpcServerEventManager(){
        return new RpcServerEventManager();
    }

    @Bean
    public RpcServerEventBroker createRpcServerEventBroker(RpcServerEventManager manager, EventListenerSettings settings){
        return new RpcServerEventBroker(manager,settings);
    }

    @Bean
    public EventListenerSettings createEventListenerSettings() {
        return mock(EventListenerSettings.class);
    }

    @Bean
    public RpcTest createRpcTest(){
        return  new RpcTestImpl();
    }

    @Bean
    public InternalServiceManageService createInternalSvrManagement() {
        InternalServiceManageService service = mock(InternalServiceManageService.class);
        var serviceDefinition = DefinitionUtil.transformServiceDefinition(RpcTest.class);
        var temp = serviceDefinition.getMethods().stream().filter(x->x.getServiceId().equalsIgnoreCase(TestNoReturnRpc)).collect(Collectors.toList());
        TestNoReturnRpcDef = temp.get(0);
        when(service.getRpcMethodDefinition(ConstVirable.TestNoReturnRpc)).thenReturn(TestNoReturnRpcDef);

        var temp1 = serviceDefinition.getMethods().stream().filter(x->x.getServiceId().equalsIgnoreCase(TestReturnRpc)).collect(Collectors.toList());
        TestReturnRpcDef = temp1.get(0);
        when(service.getRpcMethodDefinition(TestReturnRpc)).thenReturn(TestReturnRpcDef);

        var temp2 = serviceDefinition.getMethods().stream().filter(x->x.getServiceId().equalsIgnoreCase(TestReturnWithCustomeSerializeRpc)).collect(Collectors.toList());
        TestReturnWithCustomeSerializeRpcDef = temp2.get(0);
        when(service.getRpcMethodDefinition(TestReturnWithCustomeSerializeRpc)).thenReturn(TestReturnWithCustomeSerializeRpcDef);

        var temp3 = serviceDefinition.getMethods().stream().filter(x->x.getServiceId().equalsIgnoreCase(TestDownloadFile)).collect(Collectors.toList());
        TestDownloadFileDef = temp3.get(0);
        when(service.getRpcMethodDefinition(TestDownloadFile)).thenReturn(TestDownloadFileDef);

        var temp4 = serviceDefinition.getMethods().stream().filter(x->x.getServiceId().equalsIgnoreCase(TestUploadFile)).collect(Collectors.toList());
        TestUploadFileDef = temp4.get(0);
        when(service.getRpcMethodDefinition(TestUploadFile)).thenReturn(TestUploadFileDef);

        return service;
    }

}
